package com.hdu.dwh.utils;

import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * CDC 工具类：通过 Spring 注入配置中的 dwh.flink.warehouse 到静态字段，
 * 保持原有通过 CDCUtils.warehouse 访问的兼容性。
 */
@Component
public class CDCUtils {
    private static String warehouse;

    @Value("${dwh.flink.warehouse:}")
    private String warehouseConfig;

    @PostConstruct
    private void init() {
        // 将注入的配置赋值到静态字段，供静态访问使用
        CDCUtils.warehouse = this.warehouseConfig;
    }

    public static List<String> buildMysqlCDCParamList(String host, Integer port, String username, String password, String targetDb, String sourceDb, String targetTable, String sourceTable) {
        int maxTagNum = 24;

        return List.of(
            "mysql_sync_table",
            "--warehouse", warehouse,
            "--database", targetDb,
            "--table", targetTable,
            "--mysql_conf", "hostname=" + host,
            "--mysql_conf", "port=" + port,
            "--mysql_conf", "username=" + username,
            "--mysql_conf", "password=" + password,
            "--mysql_conf", "database-name=" + sourceDb,
            "--mysql_conf", "table-name=" + sourceTable,
//            "--mysql_conf", "scan.", //TODO: 扫描相关配置
            "--mysql_conf", "server-time-zone=Asia/Shanghai",
            "--table_conf", "tag.automatic-creation=process-time",
            "--table_conf", "tag.creation-period=hourly",
            "--table_conf", "tag.num-retained-max=" + maxTagNum
        );
    }

    public static List<String> buildPostgresqlCDCParamList(String host, Integer port, String username, String password, String targetDb, String sourceDb, String targetTable, String sourceTable) {
        return List.of(
                "postgres_sync_table",
                "--warehouse", warehouse,
                "--database", targetDb,
                "--table", targetTable,
                "--postgres_conf", "hostname=" + host,
                "--postgres_conf", "port=" + port,
                "--postgres_conf", "username=" + username,
                "--postgres_conf", "password=" + password,
                "--postgres_conf", "database-name=" + sourceDb,
                "--postgres_conf", "schema-name=public",
                "--postgres_conf", "table-name=" + sourceTable,
                "--postgres_conf", "decoding.plugin.name=pgoutput",
                "--postgres_conf", "slot.name=flink_slot"
        );
    }

    public static List<String> buildRadarParamList(String radarType, String endpoint, String accessKey, String secretKey, String warehouse, String database, String table, String monitorDir, Integer duration) {
        // --minio_conf monitor_dir=s3://radar/active
        // --minio_conf duration=30
        // --table_conf database=radar
        // --table_conf table=active_radar
        // --catalog_conf warehouse=s3://paimon/warehouse
        // --catalog_conf s3.endpoint=http://120.27.141.29:9000
        // --catalog_conf s3.access-key=data-warehouse2
        // --catalog_conf s3.secret-key=data-warehouse2
        return List.of(
                radarType,
                "--minio_conf", "monitor_dir=" + monitorDir,
                "--minio_conf", "duration=" + duration,
                "--table_conf", "database=" + database,
                "--table_conf", "table=" + table,
                "--catalog_conf", "warehouse=" + warehouse,
                "--catalog_conf", "s3.endpoint=" + endpoint,
                "--catalog_conf", "s3.access-key=" + accessKey,
                "--catalog_conf", "s3.secret-key=" + secretKey
        );
    }
    public static List<String> buildMongoCDCParamList(String hosts, String username, String password, String database, String collection, String targetDb, String targetTable) {
        // 构建MongoDB CDC参数列表
        List<String> params = new ArrayList<>();
        params.add("mongodb_sync_table");
        params.add("--warehouse");
        params.add(warehouse);
        params.add("--database");
        params.add(targetDb);
        params.add("--table");
        params.add(targetTable);
        params.add("--mongodb_conf");
        params.add("hosts=" + hosts);

        if (username != null && !username.isEmpty()) {
            params.add("--mongodb_conf");
            params.add("username=" + username);
        }

        if (password != null && !password.isEmpty()) {
            params.add("--mongodb_conf");
            params.add("password=" + password);
        }

        params.add("--mongodb_conf");
        params.add("database=" + database);
        params.add("--mongodb_conf");
        params.add("collection=" + collection);

        // 添加其他可选配置
        params.add("--table_conf");
        params.add("tag.automatic-creation=process-time");
        params.add("--table_conf");
        params.add("tag.creation-period=hourly");
        params.add("--table_conf");
        params.add("tag.num-retained-max=24");

        return params;
    }
}
